package com.sfh.dog.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.adapter.DailyAdapter;
import com.sfh.dog.entity.Daily;

/**
 * 主页最新
 * @author sWX229906
 *
 */
public class MainFragment extends AFragment
{
    
    private DailyAdapter adapter;
    
    private List<Daily> dailyEntities = new ArrayList<Daily>();
    
    @Override
    public void getData()
    {
        BmobQuery<Daily> query = new BmobQuery<Daily>();
        query.setSkip(10 * page);
        query.setLimit(10 * (page + 1));
        query.order("-createdAt");
        query.findObjects(getActivity(), new FindListener<Daily>()
        {
            
            @Override
            public void onSuccess(List<Daily> arg0)
            {
                if (page == 0)
                {
                    dailyEntities.clear();
                }
                dailyEntities.addAll(arg0);
                adapter.notifyDataSetChanged();
                mPullRefreshListView.onRefreshComplete();
            }
            
            @Override
            public void onError(int arg0, String arg1)
            {
                mPullRefreshListView.onRefreshComplete();
                
            }
        });
        
    }
    
    @Override
    public void getInit()
    {
        adapter = new DailyAdapter(getActivity(), dailyEntities);
        mPullRefreshListView.setAdapter(adapter);
    }
    
    @Override
    public void select(int position)
    {
        Daily entity = dailyEntities.get(position - 1);
        DogApplication.gotoDailyDetailUI(getActivity(), entity);
    }

    @Override
    public int Layout()
    {
        return R.layout.main_fragment;
    }
    
}
